<?php

namespace App\Console\Commands;

use App\Models\administration\MsgModel;
use App\Models\CustomerModel;
use App\Models\StaffModel;
use Illuminate\Console\Command;


class Tips extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tips';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**

    /**
     * Notes:员工转正、生日、合同到期提示
     * User: xiangzi
     * Date: 2025/7/14
     * Time: 下午5:24
     * @return void
     */
    public function handle()
    {
        $staff = StaffModel::where('status', 1)->get();

        $insert = [];
        foreach ($staff as $value) {
            //转正员工
            if ($value->turn_date == date('Y-m-d')) {
                $date = date('Y年m月d日');
                $insert[] = [
                    'title' => '员工转正',
                    'type' => 1,
                    'content' => '员工【 ' . $value->name . '】于' . $date . '试用期结束，请知晓！',
                    'created_at' => date('Y-m-d H:i:s'),
                ];
            }
            //生日员工
            if (date('m-d', strtotime($value->birth_date)) == date('m-d')) {
                $date = date('Y年m月d日');
                $insert[] = [
                    'title' => '员工生日',
                    'type' => 2,
                    'content' => $date . '是员工【 ' . $value->name . '】的生日，请知晓！',
                    'created_at' => date('Y-m-d H:i:s'),
                ];
            }
            //员工到期
            if ($value->contract_end_date == date('Y-m-d')) {
                $date = date('Y年m月d日');
                $insert[] = [
                    'title' => '劳动合同期满',
                    'type' => 3,
                    'content' => '员工【 ' . $value->name . '】于' . $date . '劳动合同期满，请知晓！',
                    'created_at' => date('Y-m-d H:i:s'),
                ];
            }
        }

        $customer = CustomerModel::where('review_status', 2)->get();
        foreach ($customer as $value){
            $month_after = date('Y-m-d', strtotime('+1 month'));
            if($value->contract_effect_date && $value->validity_period){
                $contract_invalid_date = date('Y-m-d', strtotime('+' . $value->validity_period . ' year', strtotime($value->contract_effect_date)));

                if($contract_invalid_date == $month_after){
                    $insert[] = [
                        'title' => '客户合同期满',
                        'type' => 4,
                        'content' => '客户【 ' . $value->name . '】于' . $contract_invalid_date . '合同期满，请知晓！',
                        'created_at' => date('Y-m-d H:i:s'),
                    ];
                }
            }
        }


        if($insert){
            MsgModel::query()->insert($insert);
        }

    }
}
